{include file="public/_header"}
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form-item">
                <div class="layui-inline">&emsp;
                    <button id="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;
                </div>
            </div>
            <table id="tableContainer" lay-filter="tableContainer"></table>
        </div>
    </div>
</div>
{include file="public/_footer"}
<script>
    layui.use(['layer', 'form', 'admin', 'table', 'util','xmSelect'],function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var admin = layui.admin;
        var util = layui.util;
        initTable();
        function initTable() {
            var url = "{:url('getData')}";
            var insTb = table.render({
                elem: '#tableContainer',
                url: url,
                page: true,
                // toolbar: ['<p>',
                //     '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                //     '</p>'].join(''),
                cellMinWidth: 100,
                cols : [[
                    {field: 'id', title: 'ID'},
                    {field: 'title', title: '角色名称'},
                    {field: 'remark', title: '备注'},
                    {field: 'create_time', title: '创建时间'},
                    {title: '操作', toolbar: '#barDemo', align: 'center', width: 200, minWidth: 200}
                ]]
            });
        }

        table.on('tool(tableContainer)', function (obj) {
            if (obj.event === 'edit') { // 修改
                window.location.href = 'edit.html?id=' + obj.data.id;
            } else if (obj.event === 'del') { // 删除
                layer.confirm('确定要删除吗？',{
                    btn: ['确定', '取消']
                },function (e) {
                    doDel(obj.data.id);
                });
            }
        });

        // table.on('tool(tableContainer)', function (obj) {
        //     if (obj.event === 'edit') { // 修改
        //         window.location.href = "add.html";
        //         // showEditModel(obj.data);
        //     } else if (obj.event === 'del') { // 删除
        //         // doDel(obj);
        //     }
        // });

        $('#add').click(function () {
            window.location.href = "add.html";
        });
        
        function doDel(id) {
            var data = {
                'id' : id
            };
            $.getJSON("{:url('del')}",data,function (res) {
                layer.open({
                    type: 1
                    ,offset: 'auto'
                    ,id: 'layerDemo' //防止重复弹出
                    ,content: '<div style="padding: 20px 100px;">'+ res.msg +'</div>'
                    ,btn: '关闭'
                    ,btnAlign: 'c' //按钮居中
                    ,shade: 0 //不显示遮罩
                    ,yes: function(){
                        window.location.href = "{:url('index')}";//'index.html';
                        layer.closeAll();
                    }
                });
            });
        }
        
    });
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>